package com.api.cases;

import com.alibaba.fastjson.JSONObject;
import com.api.util.*;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

/**
 * 测试基类
 */
public class BaseProcessor {
    String[] cellNames={"ApiId","CaseId","Params"};
    @Test(dataProvider = "datas",description = "接口测试")
    public void test(String apiId,String caseId,String params){
        //System.out.println("caseid:"+caseId+",parase:"+params);
        //替换变量：
        params= VariableConfigurationUtil.replaceParam(params);

        //根据接口编号获取接口地址
        String url = RestUtil.getUrlByApiId(apiId);
        //根据接口编号获取接口类型
        String type = RestUtil.getTypeByApiId(apiId);
        //完成接口调用，拿到接口响应报文字符串
        String result = RestUtil.process(url,type,params.trim());
        JSONObject jsonObject=JSONObject.parseObject(result);
        result=jsonObject.toJSONString().trim();

        //ActualResponseData(实际响应数据)
        //CaseId>行号,
        //使用集合对象保存数据，避免多次读数据
        ExcelUtil.saveWriteBackData("用例",caseId,"ActualResponseData",result);
        //批量执行写入数据
        ExcelUtil.writeData(PropertiesUtil.getCaseFilePath(),"用例",caseId,"ActualResponseData",result);

    }
    @AfterSuite
    public void tearDown(){
        //从配置文件中读取路径

        //批量回写测试数据

        ExcelUtil.batchWriteBackDatas(PropertiesUtil.getCaseFilePath());

    }
}
